package com.reader.api.controller.usercenter;


import com.reader.api.controller.BaseController;
import com.reader.api.entity.usercenter.APPToken;
import com.reader.api.entity.usercenter.DeviceInfo;
import com.reader.api.entity.usercenter.User;
import com.reader.api.service.usercenter.IAPPTokenService;
import com.reader.api.service.usercenter.IUserService;
import com.reader.core.annotation.IgnoreAuth;
import com.reader.core.entity.ResultMessageInfo;
import com.reader.core.utils.CommonVariable;
import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * Created by jiahaikun on 2018/11/04.
 */
@Api(value = "/api", description = "用户登录接口")
@RestController
@RequestMapping(value = "/api/")
public class LoginController extends BaseController<Object> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IAPPTokenService iAPPTokenService;
    /**
     * 登录
     *
     * @return 登录成功后 带回token信息
     * @param登录
     */
    @PostMapping("login")
    @ResponseBody
    @IgnoreAuth
    public ResultMessageInfo login(@RequestBody User user){
       // User appUser=new User();
        ValidatorUtil.validateEntity(user, AddGroup.class);//非空验证
        //从header中取设备类型，和设备号
        DeviceInfo deviceInfo=getDeviceInfo();
        APPToken token=iUserService.login(user.getMobile(),deviceInfo);
        return ResultMessageInfo.ofSuccess(token);
    }

    /**
     * 退出删除token
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("logout")
    public ResultMessageInfo logout(){
        String token= request.getHeader(CommonVariable.TOKEN);
        iAPPTokenService.expireToken(token);
        return ResultMessageInfo.ofSuccess();
    }

}
